<?php
/**
* 名称：通用信息模块控制器。
* 描述：通用信息模块相关的控制响应程式。
*/
final class Controller_Universal extends Controller_Public_Account{
    /**
    * 描述：列表视图程式。
    * @access protected
    * @return void
    */
    protected function Listview(){
        $data=array(
            'meta'=>array(
                'title'=>'信息管理',
                'button1_title'=>'发布信息',
                'button1_link'=>Route::MakeHyperlink(':universal::add'),
                'list_title'=>'信息列表'
            ),
            'base'=>self::$Database->GetLimit(
                'SELECT * FROM '.self::$Database->Table('universal').' ORDER BY id DESC',
                Route::GetArguments('page'),
                20
            ),
            'urlArguments'=>array(
                'class'=>'0'
            )
        );
        //~
        foreach($data['base']['Rows'] as &$value){
            $value['class_name']=Model_Universal::GetClassName($value['class']);
        }
        //~
        self::$Template->Assign('data',$data);
        self::$Template->Display('oui/universal/list.html');
    }

    /**
    * 描述：添加之视图程式。
    * @access protected
    * @return void
    */
    protected function Add(){
        $data=array(
            'meta'=>array(
                'title'=>'发布信息',
                'action'=>Route::MakeHyperlink(':universal::insert')
            ),
            'class'=>self::$Database->GetAll(
                'SELECT * FROM '.self::$Database->Table('universal_class').' ORDER BY id ASC'
            )
        );
        self::$Template->Assign('data',$data);
        self::$Template->Display('oui/universal/form.html');
    }

    /**
    * 描述：添加之响应程式。
    * @access protected
    * @return void
    */
    protected function Insert(){
        $data=array(
            'title'=>post('title'),'content'=>post('content'),'author'=>post('author'),
            'source'=>post('source'),'label'=>post('label'),'summary'=>post('summary'),
            'datetime'=>post('datetime'),'class'=>post('class'),'cover'=>post('cover'),
            'display'=>post('display')
        );
        //~
        if(empty($data['datetime'])){
            $data['datetime']=now();
        }
        //~
        $this->Feedback(
            self::$Database->Insert(self::$Database->Table('universal'),$data),
            array(
                'notice'=>'信息发布成功，3秒钟后自动返回。',
                'link'=>array(
                    array('name'=>'继续发布','href'=>Route::MakeHyperlink(':universal::add')),
                    array('name'=>'前往修改','href'=>Route::MakeHyperlink(':universal::edit','id:'.self::$Database->lastInsertId())),
                    array('name'=>'返回列表','href'=>Route::MakeHyperlink(':universal::listview'))
                ),
                'autoTurn'=>array(TRUE,3)
            ),
            '发布失败，请稍候再试！'
        );
    }

    /**
    * 描述：修改之视图程式。
    * @access protected
    * @return void
    */
    protected function Edit(){
        $data=array(
            'meta'=>array(
                'title'=>'修改信息',
                'action'=>Route::MakeHyperlink(':universal::update')
            ),
            'base'=>self::$Database->GetRow(
                'SELECT * FROM '.self::$Database->Table('universal').' WHERE id=?',
                array(Route::GetArguments('id'))
            ),
            'class'=>self::$Database->GetAll(
                'SELECT * FROM '.self::$Database->Table('universal_class').' ORDER BY ID ASC'
            )
        );
        self::$Template->Assign('data',$data);
        self::$Template->Display('oui/universal/form.html');
    }

    /**
    * 描述：修改之响应程式。
    * @access protected
    * @return void
    */
    protected function Update(){
        $data=array(
            'base'=>array(
                'title'=>post('title'),'content'=>post('content'),'author'=>post('author'),
                'source'=>post('source'),'label'=>post('label'),'summary'=>post('summary'),
                'datetime'=>post('datetime'),'class'=>post('class'),'cover'=>post('cover'),
                'display'=>post('display')
            ),
            'condition'=>array(
                'sql'=>'id=?',
                'parameters'=>array(post('id'))
            )
        );
        //~
        $this->Feedback(
            self::$Database->Update(self::$Database->Table('universal'),$data['base'],$data['condition']),
            array(
                'notice'=>'信息修改成功，3秒钟后自动返回。',
                'link'=>array(
                    array('name'=>'继续修改','href'=>Route::MakeHyperlink(':universal::edit','id:'.post('id'))),
                    array('name'=>'返回列表','href'=>Route::MakeHyperlink(':universal::listview'))
                ),
                'autoTurn'=>array(TRUE,3)
            ),
            '修改失败，请稍候再试！'
        );
    }

    /**
    * 描述：信息删除程式之删除。
    * @access protected
    * @return void
    */
    protected function Delete(){
        $id=Route::GetArguments('id');
        //~
        $this->Feedback(
            self::$Database->ExecParameterized(
                'DELETE FROM '.self::$Database->Table('universal').' WHERE id=?',array($id)
            ),
            $_SERVER['HTTP_REFERER'],
            '删除失败，请稍候再试！'
        );
    }
}